﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Transactions;
using Components;
using Model;
using MsDAL;
using System.ComponentModel;
using ServiceHelper.Util;

namespace ServiceHelper
{
    [DataObjectAttribute]
    public partial class Tic1a10Manager : Tic1a10Base
    {
        private static Tic1a10Manager _entity;

        public static Tic1a10Manager Entity
        {
            get
            {
                if (_entity == null)
                    _entity = new Tic1a10Manager();
                return _entity;
            }
        }

        #region 个人代码

        public string SaveData(Hashtable tb, DataTable dtl, DataTable deldata, string action, string usercode)
        {
            var lst = new List<SqlApp>();

            Tic1a10 model = null;
            if ("A" == action && (dtl == null || dtl.Rows.Count == 0)) return "明细中没有数据，不需要保存！";

            string appstr = Context.GetAppByUserCode(usercode);

            using (var tran = new TransactionScope())
            {
                if ("A" == action) //新增
                {
                    var sellerId = Validation.ToString(tb["cesellerid"]);

                    foreach (DataRow row in dtl.Rows)
                    {

                        model = new Tic1a10();
                        Components.CommonUtil.GetInstance.CopyProperties(model, row);

                        if (IsExistsDOcno(model.Cedono))
                            return string.Format("单号{0}已存在，请调整", model.Cedono);

                        model.Cesellerid = sellerId;
                        model.Ceid = Sys0a60Manager.Entity.CreateSn("tic1a10_id", usercode);
                        model.Cecreatetime = DateTime.Now;
                        model.Ceusercode = appstr;
                        model.Ceappid = ServerContext.ComCode;
                        model.Ceautono = Sys0a60Manager.Entity.CreateSn("tic1a10_docno", usercode);

                        var amout = Validation.ToFloat(model.Ceamount);
                        if (model.Cesubtype == 2) //支出
                        {
                            amout = 0 - amout;
                        }

                        lst.Add(Bas1a10Manager.Entity.UpdateBalance(model.Cesellerid, amout));

                        lst.Add(this.Add(model));
                    }
                }

                var msg = CommonManager.Entity.SaveData(lst);

                if (string.IsNullOrEmpty(msg)) //success
                {
                    tran.Complete();
                }

                return msg;
            }
        }

        private bool IsExistsDOcno(string docno)
        {
            var qb = QB.Entity
                .AddParam("cedono", docno)
                .AddParam("ceappid", ServerContext.ComCode);

            return this.Exists("cedono=@cedono and ceappid=@ceappid", qb);
        }

        public string SaveEditData(Hashtable tb, string action, string usercode)
        {
            var lst = new List<SqlApp>();

            Tic1a10 model = null;
            string appstr = Context.GetAppByUserCode(usercode);

            using (var tran = new TransactionScope())
            {
                if ("M" == action)
                {
                    model = this.GetModelById(Components.Validation.ToString(tb["ceid"]));
                    if (null == model) return "该编辑记录可能已经被删除！";

                    var amout = Validation.ToFloat(model.Ceamount);
                    //先原因以前的余额
                    if (model.Cesubtype == 1)
                    {
                        amout = 0 - amout;
                    }

                    lst.Add(Bas1a10Manager.Entity.UpdateBalance(model.Cesellerid, amout));

                    Components.CommonUtil.GetInstance.CopyProperties(tb, model);
                    model.Ceusercode = appstr;

                    amout = Validation.ToFloat(model.Ceamount);
                    if (model.Cesubtype == 2) //支出
                    {
                        amout = 0 - amout;
                    }

                    lst.Add(Bas1a10Manager.Entity.UpdateBalance(model.Cesellerid, amout));

                    lst.Add(this.Update(model));
                }

                var msg = CommonManager.Entity.SaveData(lst);

                if (string.IsNullOrEmpty(msg)) //success
                {
                    tran.Complete();
                }

                return msg;
            }
        }

        #endregion
    }
}



